package cn.huiyunche.admin.service.interfaces.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.huiyunche.admin.service.interfaces.DLinesService;
import cn.huiyunche.base.service.mappers.DLinesMapper;
import cn.huiyunche.base.service.model.DLines;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;

import com.alibaba.fastjson.JSON;

@Service
public class DLinesServiceImpl implements DLinesService{

	private static final Logger logger = LoggerFactory.getLogger(DLinesServiceImpl.class);
	
	@Autowired
	private DLinesMapper dLinesMapper = null;
	
	private DLinesMapper getDLinesMapper() {
		return this.dLinesMapper;
	}
	
	/**
	 * 分页查询线路列表
	 */
	@Override
 	public Map<String, Object> getList(PageVo page, DLines dlines) {
		logger.info("select dlines params : {}, {}", page, dlines);
		List<DLines> list = new ArrayList<DLines>();
		
		dlines.setLimitStart(page.getStartIndex());
		dlines.setLimitEnd(page.getPageSize());
		if (StringUtils.isNoneBlank(page.getOrder())) {
			dlines.setOrderByClause(page.getOrder());
		} else {
			dlines.setOrderByClause(" id DESC");
		}
		try {
			page.setTotalRecord(this.getDLinesMapper().countByCondations(dlines));
		} catch (Exception e) {
			logger.error("select countByCondations error : {}", e);
			throw new BusinessException("查询总记录数异常");
		}
		try {
			list = this.getDLinesMapper().selectByConditions(dlines);
		} catch (Exception e) {
			logger.error("select dlinesinfo error : {}", e);
			throw new BusinessException("查询线路信息错误");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("dLinesList", list);
		map.put("page", page);
		return map;
	}
	
	/**
	 * 对线路列表添加操作
	 */
	@Override
	public Result<String> addDLines(DLines dlines) {
		logger.info("add dlines params : {}.", dlines);
		Result<String> r = new Result<>(true);
		try {
			logger.info("add dlines params : {}.", JSON.toJSONString(dlines));
			if(null != dlines) {
				if(StringUtils.isNotBlank(dlines.getFromProvince())) {
					String fromProvince = this.getDLinesMapper().getAreaCode(dlines.getFromProvince());
					if(StringUtils.isNotBlank(fromProvince))
						dlines.setFromCode(fromProvince);
				}
				if(StringUtils.isNotBlank(dlines.getToProvince())) {
					String toProvince = this.getDLinesMapper().getAreaCode(dlines.getToProvince());
					if(StringUtils.isNotBlank(toProvince))
						dlines.setToCode(toProvince);
				}
				Date d = new Date();
				dlines.setCreateTime(d);
				dlines.setUpdateTime(d);
				this.getDLinesMapper().insert(dlines);
			}
			return r;
		} catch(Exception e) {
			logger.info("add dlines params failed.", e);
			throw new BusinessException("线路列表添加操作失败");
		}
	}

	/**
	 * 对线路列表修改操作
	 */
	@Override
	public Map<String, Object> modifyDLines(DLines dlines) {
		logger.info("modify dlines params : {}.", dlines);
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String fromProvince = dlines.getFromProvince();
			if(StringUtils.isNotBlank(fromProvince)) {
				String fromCode = this.getDLinesMapper().getAreaCode(fromProvince);
				dlines.setFromCode(fromCode);
			}
			String toProvince = dlines.getToProvince();
			if(StringUtils.isNotBlank(toProvince)) {
				String toCode = this.getDLinesMapper().getAreaCode(toProvince);
				dlines.setToCode(toCode);
			}
			dlines.setUpdateTime(new Date());
			
			this.getDLinesMapper().updateByPrimaryKeySelective(dlines);
			map.put("true", true);
		} catch (Exception e) {
			logger.info("modify dlines params failed.", e);
			map.put("false", false);
		}
		return map;
	}
	

}
